package com.jarvis.hospital;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.jarvis.hospital.common.AppManager;
import com.jarvis.hospital.common.BaseActivity;
import com.jarvis.hospital.common.ResponseData;
import com.jarvis.hospital.login.LoginActivity;
import com.jarvis.hospital.login.model.LoginModel;
import com.jarvis.hospital.login.model.UserModel;
import com.jarvis.hospital.patient.MainPatientActivity;
import com.jarvis.hospital.prescription.AllPrescriptionListActivity;
import com.jarvis.hospital.prescription.DefaultPrescriptionListActivity;
import com.jarvis.hospital.prescription.model.UnreadCountModel;
import com.jarvis.hospital.servers.ApiService;
import com.jarvis.hospital.util.SharePreferenceKey;
import com.jarvis.hospital.util.SharePreferenceUtils;
import com.jarvis.hospital.util.net.RequestApi;

import butterknife.BindView;
import cn.jpush.android.api.JPushInterface;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class SettingActivity extends BaseActivity implements View.OnClickListener{

    @BindView(R.id.rel_left)
    RelativeLayout rel_left;
    @BindView(R.id.tv_title)
    TextView tv_title;
    @BindView(R.id.tv_name)
    TextView tv_name;
    @BindView(R.id.ivHead)
    ImageView ivHead;
    @BindView(R.id.tv_loginout)
    TextView tv_loginout;

    private ApiService service;
    @Override
    public int getLayoutId() {
        return R.layout.activity_setting;
    }

    @Override
    public void initData() {
        service = RequestApi.create(ApiService.class);
    }

    @Override
    public void initView() {
        rel_left.setOnClickListener(this);
        tv_loginout.setOnClickListener(this);
        ivHead.setOnClickListener(this);
        tv_title.setText("设置");

    }

    @Override
    protected void onResume() {
        super.onResume();
        UserModel userModel = SharePreferenceUtils.getObject(SharePreferenceKey.LOGIN_MODEL, LoginModel.class).getUserInfo();
        tv_name.setText(userModel.getUserName());
        Glide.with(this)
                .load(userModel.getUserHeadImg())
                .error(R.drawable.img_main_bg)
                .into(ivHead);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rel_left:
                finish();
                break;
            case R.id.ivHead:
                startActivity(ImageActivity.class);
                break;
            case R.id.tv_loginout:
                loginout();
                break;
        }
    }
    public void loginout() {
        showDialog("加载中");
        service.loginOut("0")
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .safeSubscribe(new Observer<ResponseData>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(ResponseData responseData) {
                        if (responseData != null) {
                            int resultCode = responseData.getResultCode();
                            if (resultCode == 200) {
                                AppManager.getAppManager().finishAllActivity();
                                UserModel userModel = SharePreferenceUtils.getObject(SharePreferenceKey.LOGIN_MODEL, LoginModel.class).getUserInfo();
                                JPushInterface.deleteAlias(SettingActivity.this,Integer.parseInt(userModel.getUserId()));
//                                JPushInterface.cleanTags(SettingActivity.this,Integer.parseInt(userModel.getUserId()));
                                SharePreferenceUtils.remove(SharePreferenceKey.LOGIN_MODEL);
                                startActivity(LoginActivity.class);
                            } else {
                                showTextToast(responseData.getMessage());
                            }
                        } else {
                            showTextToast("请求失败");
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        disMiss();
                        showTextToast(RequestApi.detailError(e));
                    }

                    @Override
                    public void onComplete() {
                        disMiss();
                    }
                });

    }
}
